package cn.bonoon.controllers.faq;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bonoon.core.plugins.FaqService;
import cn.bonoon.entities.interacting.FaqEntity;
import cn.bonoon.kernel.web.ButtonEventType;
import cn.bonoon.kernel.web.ButtonRefreshType;
import cn.bonoon.kernel.web.annotations.grid.GridStandardDefinition;
import cn.bonoon.kernel.web.controllers.AbstractGridController;
import cn.bonoon.kernel.web.models.JsonResult;

@Controller
@RequestMapping("/s/sys/faq")
public class FaqController extends AbstractGridController<FaqEntity, FaqItem> {
	private final FaqService service;

	@Autowired
	public FaqController(FaqService service) {
		super(service);
		this.service = service;
	}

	@Override
	protected Class<FaqItem> itemClass() {
		return FaqItem.class;
	}

	@Override
	@GridStandardDefinition(
			insertClass = FaqEditor.class, 
			updateClass = FaqEditor.class, 
			detailClass = FaqDetail.class)
	protected FaqService initLayoutGrid(LayoutGridRegister register) throws Exception {
		register.button("发布/取消", "changeStatus.do", ButtonEventType.GET,ButtonRefreshType.FINISH);
		//register.setDefaultDialogWidth(740);
		// 添加按键
//		DialogInsertHandler.toolbar(register, service, FAQEditor.class);
//		OperateIdsHandler.delete(register, service);
//
//		DialogDetailHandler.button(register, service, FAQDetail.class);
//		DialogUpdateHandler.button(register, service, FAQEditor.class);
//		OperateIdHandler.delete(register, service);
		return service;
	}	
	
	@ResponseBody
	@RequestMapping(value = "!{key}/changeStatus.do", method = { POST, GET })
	public Object changeReleaseStatus(HttpServletRequest request, 
			@PathVariable("key") String key, 
			@RequestParam("id") Long id) {
		try{
			service.changeReleaseStatus(getUser(), id);
			return JsonResult.result();
		}catch (Exception e) {
			return JsonResult.error(e);
		}
	}
}
